<?php

function file_size($size) {
    if ($size < 1024) {
        return round($size, 2) . ' B';
    }
    if ($size < (1024 * 1024)) {
        return round($size / 1024, 2) . ' KB';
    }
    if ($size < (1024 * 1024 * 1024)) {
        return round($size / (1024 * 1024), 2) . ' MB';
    } else {
        return round($size / (1024 * 1024 * 1024), 2) . ' GB';
    }
}

function file_type($type){
    $file = explode('.', $type);
    return end($file);
}
?>
<?php foreach ($categories as $cat): ?>
    <?php $files = ORM::factory('downloads')->where('category_id', '=', $cat->id)->find_all(); ?>
    <?php if ($files->count() > 0): ?>
        <p><b><?php echo $cat->name ?></b></p>
        <div width="400" align="center">  
            <table class="table_head"><tr><td align="left">&nbsp;</td></tr></table>
            <table class="table_body">
                <tr>
                    <td width="100" align="center">Tartalom</td>
                    <td width="50" align="center">Típus</td>
                    <td width="75" align="center">Méret</td>
                    <td width="75" align="center">Feltöltve</td>
                </tr>
                <?php foreach ($files as $file) : ?>
                    <tr>
                        <td align="left"><a href="<?php echo URL::base() . 'uploads/' . $file->file_name ?>"><?php echo $file->description ?></a></td>
                        <td align="center"><?php echo file_type($file->file_name) ?></td>
                        <td align="center"><?php echo file_size($file->size) ?></td>
                        <td align="center"><?php echo $file->uploaded; ?></td>
                    </tr>
        <?php endforeach ?>
            </table>
            <table class="table_foot">
                <tr>
                    <td align="left">&nbsp;</td>
                </tr>
            </table>
        </div>
    <?php endif ?>
<?php endforeach ?>